package sust.cse.ruhul.jobs1;

import java.io.IOException;
import java.lang.ref.WeakReference;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

public class ImagePreview extends Activity {
	private ImageView previewImage;
	int w, h;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.imagepreview);

		previewImage = (ImageView) findViewById(R.id.preview_image);

		final Intent intent = getIntent();
		// final Point size = getSize();
		int w = getResources().getDisplayMetrics().widthPixels;
		int h = getResources().getDisplayMetrics().heightPixels;

		// load image in the background:
		final Uri data = intent.getData();
		final String filePath = data.getPath();
		final BitmapWorkerTask task = new BitmapWorkerTask(this, w, h);
		task.execute(filePath);

		// previewImage.setImageBitmap(decodeSampledBitmapFromResource(filePath,
		// size.x , size.y ));
	}

	private class BitmapWorkerTask extends AsyncTask<String, Integer, Bitmap> {
		private final WeakReference<ImagePreview> activityReference;
		private String filePath = null;
		private int reqWidth;
		private int reqHeight;

		public BitmapWorkerTask(final ImagePreview activity,
				final int reqWidth, final int reqHeight) {
			// Use a WeakReference to ensure the ImageView can be garbage
			// collected
			activityReference = new WeakReference<ImagePreview>(activity);
			this.reqWidth = reqWidth;
			this.reqHeight = reqHeight;
		}

		@Override
		protected Bitmap doInBackground(final String... filePaths) {
			filePath = filePaths[0];

			// first decode with inJustDecodeBounds=true to check dimensions
			final BitmapFactory.Options options = new BitmapFactory.Options();
			options.inJustDecodeBounds = true;
			BitmapFactory.decodeFile(filePath, options);

			// calculate the sample size:
			int inSampleSize = 1;
			int imgWidth = options.outWidth;
			int imgHeight = options.outHeight;

			ExifInterface exif;
			int orientation = 0;
			try {
				exif = new ExifInterface(filePath);
				orientation = exif.getAttributeInt(
						ExifInterface.TAG_ORIENTATION,
						ExifInterface.ORIENTATION_NORMAL);
				if (orientation == android.media.ExifInterface.ORIENTATION_ROTATE_90) {
					orientation = 90;
				} else if (orientation == android.media.ExifInterface.ORIENTATION_ROTATE_180) {
					orientation = 180;
				} else if (orientation == android.media.ExifInterface.ORIENTATION_ROTATE_270) {
					orientation = 270;
				}
				if (imgWidth > imgHeight) {
					int temp = imgWidth;
					imgWidth = imgHeight;
					imgHeight = temp;
				}
				Log.e("orientation ", " " + orientation);
				Log.e("imgWidth ", " " + imgWidth);
				Log.e("imgHeight ", " " + imgHeight);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (imgHeight > reqHeight || imgWidth > reqWidth) {
				if (imgWidth > imgHeight) {
					inSampleSize = Math.round((float) imgHeight
							/ (float) reqHeight);
					System.out.println("------------------------- > "
							+ inSampleSize);
				} else {
					inSampleSize = Math.round((float) imgHeight
							/ (float) reqWidth);
					System.out.println("------------------------- < "
							+ inSampleSize);
				}
			}
			System.out.println("------------------------- " + inSampleSize
					+ " -- " + imgWidth + " " + imgHeight + " --- " + reqWidth
					+ " " + reqHeight);
			options.inSampleSize = inSampleSize;
			options.inJustDecodeBounds = false;
			// w = imgWidth;
			// h = imgHeight;
			final Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);
			// if(orientation==90){
			// bitmap.
			// }
//			return bitmap;
			Bitmap rotatedBitmap = bitmap;
			Matrix matrix = new Matrix();
			matrix.setRotate(orientation);
			rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
					options.outWidth, options.outHeight, matrix, true);
			return rotatedBitmap;
		}

		// Once complete, see if ImagePreviewActivity is still around and set
		// bitmap.
		@Override
		protected void onPostExecute(final Bitmap bitmap) {
			if (activityReference != null && bitmap != null) {
				final ImagePreview activity = activityReference.get();
				if (activity != null) {
					activity.setLoadedBitmap(bitmap);
				}
			}
		}
	}

	private void setLoadedBitmap(final Bitmap bitmap) {
		// previewImage.setVisibility(View.VISIBLE);

		// final LinearLayout.LayoutParams p = new
		// LinearLayout.LayoutParams(dpToPx(w), dpToPx(h));
		// previewImage.setLayoutParams(p);
		previewImage.setImageBitmap(bitmap);
	}

	// @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
	// public Point getSize()
	// {
	// final Point size = new Point();
	// if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.HONEYCOMB_MR2)
	// {
	// final Display display = ImagePreview.this.getWindowManager()
	// .getDefaultDisplay();
	// size.x = display.getWidth();
	// size.y = display.getHeight();
	// }
	// else
	// {
	// ImagePreview.this.getWindowManager().getDefaultDisplay()
	// .getSize(size);
	// }
	// return size;
	//
	// }

	public float getDensity() {

		return ImagePreview.this.getResources().getDisplayMetrics().density;

	}

	public int dpToPx(final int dp) {
		return (int) Math.round(getDensity() * dp);
	}

}